import { requestClient } from '#/api/request';

export namespace AiChatApi {
  /** 消息类型 */
  export type MessageType = 'text' | 'image' | 'file' | 'system';

  /** 消息角色 */
  export type MessageRole = 'user' | 'assistant' | 'system';

  /** 聊天消息 */
  export interface ChatMessage {
    id: string;
    role: MessageRole;
    content: string;
    type: MessageType;
    timestamp: number;
    sessionId: string;
    attachments?: MessageAttachment[];
    metadata?: Record<string, any>;
  }

  /** 消息附件 */
  export interface MessageAttachment {
    id: string;
    name: string;
    type: string;
    size: number;
    url: string;
  }

  /** 聊天会话 */
  export interface ChatSession {
    id: string;
    title: string;
    createdAt: number;
    updatedAt: number;
    messageCount: number;
    lastMessage?: string;
    model?: string;
    settings?: ChatSettings;
  }

  /** 聊天设置 */
  export interface ChatSettings {
    model: string;
    temperature: number;
    maxTokens: number;
    systemPrompt?: string;
    enableHistory: boolean;
    maxHistoryLength: number;
  }

  /** 发送消息参数 */
  export interface SendMessageParams {
    sessionId: string;
    content: string;
    type?: MessageType;
    attachments?: File[];
    parentMessageId?: string;
  }

  /** 发送消息返回值 */
  export interface SendMessageResult {
    message: ChatMessage;
    sessionId: string;
  }

  /** 创建会话参数 */
  export interface CreateSessionParams {
    title?: string;
    model?: string;
    settings?: Partial<ChatSettings>;
  }

  /** 创建会话返回值 */
  export interface CreateSessionResult {
    session: ChatSession;
  }

  /** 获取会话列表参数 */
  export interface GetSessionsParams {
    page?: number;
    pageSize?: number;
    search?: string;
  }

  /** 获取会话列表返回值 */
  export interface GetSessionsResult {
    sessions: ChatSession[];
    total: number;
    page: number;
    pageSize: number;
  }

  /** 获取消息历史参数 */
  export interface GetMessagesParams {
    sessionId: string;
    page?: number;
    pageSize?: number;
    beforeTimestamp?: number;
  }

  /** 获取消息历史返回值 */
  export interface GetMessagesResult {
    messages: ChatMessage[];
    total: number;
    page: number;
    pageSize: number;
    hasMore: boolean;
  }

  /** 更新会话参数 */
  export interface UpdateSessionParams {
    id: string;
    title?: string;
    settings?: Partial<ChatSettings>;
  }

  /** AI模型信息 */
  export interface AiModel {
    id: string;
    name: string;
    description: string;
    provider: string;
    maxTokens: number;
    supportedTypes: MessageType[];
    pricing?: {
      input: number;
      output: number;
    };
  }

  /** 获取可用模型返回值 */
  export interface GetModelsResult {
    models: AiModel[];
  }

  /** 流式响应数据 */
  export interface StreamResponse {
    id: string;
    content: string;
    finished: boolean;
    error?: string;
  }
}

/**
 * 创建聊天会话
 */
export async function createSessionApi(params: AiChatApi.CreateSessionParams) {
  return requestClient.post<AiChatApi.CreateSessionResult>('/ai/chat/sessions', params);
}

/**
 * 获取聊天会话列表
 */
export async function getSessionsApi(params?: AiChatApi.GetSessionsParams) {
  return requestClient.get<AiChatApi.GetSessionsResult>('/ai/chat/sessions', { params });
}

/**
 * 获取会话详情
 */
export async function getSessionApi(sessionId: string) {
  return requestClient.get<AiChatApi.ChatSession>(`/ai/chat/sessions/${sessionId}`);
}

/**
 * 更新会话信息
 */
export async function updateSessionApi(params: AiChatApi.UpdateSessionParams) {
  const { id, ...data } = params;
  return requestClient.put<AiChatApi.ChatSession>(`/ai/chat/sessions/${id}`, data);
}

/**
 * 删除会话
 */
export async function deleteSessionApi(sessionId: string) {
  return requestClient.delete(`/ai/chat/sessions/${sessionId}`);
}

/**
 * 发送消息
 */
export async function sendMessageApi(params: AiChatApi.SendMessageParams) {
  return requestClient.post<AiChatApi.SendMessageResult>('/ai/chat/messages', params);
}

/**
 * 获取消息历史
 */
export async function getMessagesApi(params: AiChatApi.GetMessagesParams) {
  return requestClient.get<AiChatApi.GetMessagesResult>('/ai/chat/messages', { params });
}

/**
 * 删除消息
 */
export async function deleteMessageApi(messageId: string) {
  return requestClient.delete(`/ai/chat/messages/${messageId}`);
}

/**
 * 获取可用AI模型
 */
export async function getModelsApi() {
  return requestClient.get<AiChatApi.GetModelsResult>('/ai/chat/models');
}

/**
 * 流式发送消息
 */
export function sendMessageStreamApi(params: AiChatApi.SendMessageParams) {
  return requestClient.post('/ai/chat/messages/stream', params, {
    responseType: 'stream',
  });
}

/**
 * 重新生成回复
 */
export async function regenerateMessageApi(messageId: string) {
  return requestClient.post<AiChatApi.SendMessageResult>(`/ai/chat/messages/${messageId}/regenerate`);
}

/**
 * 停止生成
 */
export async function stopGenerationApi(sessionId: string) {
  return requestClient.post(`/ai/chat/sessions/${sessionId}/stop`);
}

/**
 * 清空会话历史
 */
export async function clearSessionApi(sessionId: string) {
  return requestClient.delete(`/ai/chat/sessions/${sessionId}/messages`);
}

/**
 * 导出会话
 */
export async function exportSessionApi(sessionId: string, format: 'json' | 'markdown' | 'txt' = 'json') {
  return requestClient.get(`/ai/chat/sessions/${sessionId}/export`, {
    params: { format },
    responseType: 'blob',
  });
}